package com.csmht.tododemo.until;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

public class JwtUntil {

    public static String createJWT(String key, long ttl, Map<String,Object> claims ) {

        long expMillis = System.currentTimeMillis() + ttl;
        Date exp = new Date(expMillis);

        SecretKey key1 = Keys.hmacShaKeyFor(key.getBytes(StandardCharsets.UTF_8));

        JwtBuilder builder = Jwts.builder()
                .signWith(key1)
                .claims(claims)
                .expiration(exp);
        return builder.compact();
    }


    public static Claims parseJWT(String key,String token) {
        SecretKey key1 = Keys.hmacShaKeyFor(key.getBytes(StandardCharsets.UTF_8));

        JwtParser jwtParser = Jwts.parser()
                .verifyWith(key1)
                .build();
        Jws<Claims> jws = jwtParser.parseSignedClaims(token);
        return jws.getPayload();
    }



}
